package com.caigou.modules.gallery.ui.comment

import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseCommentDetailActivity
import com.caigou.comm.base.ext.*
import com.caigou.comm.base.widget.ReportDialog
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.gallery.R
import com.caigou.modules.gallery.adpter.CommentDetailItemAdapter
import com.caigou.modules.gallery.viewmodel.CommentViewModel
import com.caigou.modules.gallery.viewmodel.LikeViewModel
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NgvChildImageView
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/19
 */
@Route(path = RouterPath.Gallery.PAGE_COMMENT_DETAIL)
@AndroidEntryPoint
class GalleryCommentDetailActivity : BaseCommentDetailActivity() {

    private val commentViewModel: CommentViewModel by viewModels()
    private val likeViewModel: LikeViewModel by viewModels()

    @JvmField
    @Autowired(name = AppConstants.DATA_TYPE)
    var type = 1

    private val mAdapter: DefaultNgvAdapter<String> by lazy {
        DefaultNgvAdapter(100, GlideDisplay())
    }

    private val itemAdapter: CommentDetailItemAdapter by lazy {
        CommentDetailItemAdapter(this, type, mutableListOf())
    }

    override fun initView() {
        super.initView()

        binding.commentDetailBtnReply.setOnClickListener {
            if (AppBackgroundInfo.instance.hasLogin) {
                ARouter.getInstance()
                    .build(RouterPath.Gallery.PAGE_COMMENT_REPLY)
                    .withObject(AppConstants.COMMENT_ITEM_BEAN, commentItemBean)
                    .withObject(AppConstants.COMMENT_ROOT_ITEM_BEAN, commentItemBean)
                    .withInt(AppConstants.DATA_TYPE, type)
                    .navigation()
            } else {
                showToast("请您先登录")
            }
        }


        itemAdapter.setOnItemChildClickListener { _, view, position ->
            val item = itemAdapter.data[position]
            when (view.id) {
                R.id.item_comment_detail_iv_avatar -> {
                    ARouter.getInstance().build(RouterPath.Personal.PAGE_PERSONAL_PAGE)
                        .withString(AppConstants.USER_ID, item.replyUserInfoVo.userId)
                        .navigation()
                }
                R.id.item_comment_detail_tv_report -> {
                    if (AppBackgroundInfo.instance.hasLogin) {
                        with(ReportDialog(this)) {
                            btnConfirm.setOnClickListener {
                                if (TextUtils.isEmpty(reportCause)) {
                                    showToast("举报原因不能为空！")
                                    return@setOnClickListener
                                }
                                reportViewModel.setReplyReport(
                                    AppBackgroundInfo.instance.userBean!!.userId, item.replyUserInfoVo.userId,
                                    reportCause, item.replyId, 5
                                )
                                dismiss()
                            }
                            show()
                        }
                    } else {
                        ARouter.getInstance().build(RouterPath.Login.PAGE_LOGIN).navigation()
                    }
                }
            }
        }

        handleCommentUI()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(commentViewModel.commentDetailLiveData, ::handleGetCommentDetail)
        observe(likeViewModel.commentLikeLiveData, ::handleSetCommentLike)
        observe(reportViewModel.replyReportLiveData, ::handleSetReport)
    }

    override fun handleCommentUI() {
        super.handleCommentUI()

        binding.commentDetailTvReport.setOnClickListener {
            if (AppBackgroundInfo.instance.hasLogin) {
                with(ReportDialog(this)) {
                    btnConfirm.setOnClickListener {
                        if (TextUtils.isEmpty(reportCause)) {
                            showToast("举报原因不能为空！")
                            return@setOnClickListener
                        }
                        reportViewModel.setReplyReport(
                            AppBackgroundInfo.instance.userBean!!.userId, commentItemBean.replyUserInfoVo.userId,
                            reportCause, commentItemBean.replyId, 5
                        )
                        dismiss()
                    }
                    show()
                }
            } else {
                ARouter.getInstance().build(RouterPath.Login.PAGE_LOGIN).navigation()
            }
        }

        binding.commentDetailIvLike.setOnClickListener {
            if (AppBackgroundInfo.instance.hasLogin) {
                if (commentItemBean.like) {
                    showToast("您已经点过赞了")
                } else {
                    likeViewModel.setCommentLike(
                        commentItemBean.type, commentItemBean.replyId,
                        AppBackgroundInfo.instance.userBean!!.userId,
                        commentItemBean.replyUserInfoVo.userId
                    )
                }
            } else {
                showToast("请您先登录")
            }
        }

        if (commentItemBean.replyChildren.isNullOrEmpty()) {
            binding.commentDetailLlEmpty.visibility = View.VISIBLE
            binding.commentDetailRecyclerView.visibility = View.GONE
        } else {
            binding.commentDetailLlEmpty.visibility = View.GONE
            binding.commentDetailRecyclerView.visibility = View.VISIBLE
        }

        if (commentItemBean.imgs.size > 0) {
            binding.commentDetailNgvDisplay.run {
                visibility = View.VISIBLE
                setSingleImageSize(TypedValue.COMPLEX_UNIT_DIP, 150, 200)
                mAdapter.dataList = commentItemBean.imgs
                mAdapter.setOnChildClickListener(object : DefaultNgvAdapter.OnChildClickedListener<String> {
                    override fun onPlusImageClicked(plusImageView: ImageView?, dValueToLimited: Int) {

                    }

                    override fun onContentImageClicked(position: Int, data: String?, childImageView: NgvChildImageView?) {
                        data?.apply {
                            showImgsPreview(this@GalleryCommentDetailActivity, commentItemBean.imgs, commentItemBean.imgs.indexOf(this))
                        }
                    }

                    override fun onImageDeleted(position: Int, data: String?) {
                    }
                })
                adapter = mAdapter
            }
        }

        binding.commentDetailRecyclerView.run {
            layoutManager = LinearLayoutManager(this@GalleryCommentDetailActivity)
            adapter = itemAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@GalleryCommentDetailActivity))
        }
        itemAdapter.setList(commentItemBean.replyChildren)
    }

    override fun fetchCommentDetail() {
        super.fetchCommentDetail()
        commentViewModel.getCommentDetail(
            commentItemBean.dataId, commentItemBean.detailId, type,
            commentItemBean.replyId, commentItemBean.replyUserInfoVo.userId
        )
    }
}